1 Effect of UPSTM-Based Decorrelation on Feature Discovery

1.0.1 Loading the libraries

library("FRESA.CAD")
library(readxl)
library(igraph)
library(umap)
library(tsne)
library(entropy)

op <- par(no.readonly = TRUE)
pander::panderOptions('digits', 3)
pander::panderOptions('table.split.table', 400)
pander::panderOptions('keep.trailing.zeros',TRUE)

1.1 Material and Methods

Data Source https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6960825/

Mohino-Herranz I, Gil-Pita R, Rosa-Zurera M, Seoane F. Activity Recognition Using Wearable Physiological Measurements: Selection of Features from a Comprehensive Literature Study. Sensors (Basel). 2019 Dec 13;19(24):0. doi: 10.3390/s19245524. PMID: 31847261; PMCID: PMC6960825.

1.2 The Data

Activitydata <- read.csv("~/GitHub/LatentBiomarkers/Data/ActivityData/data.txt", header=FALSE, stringsAsFactors=TRUE)
featNames <- read.table("~/GitHub/LatentBiomarkers/Data/ActivityData/Featurelabels.txt", quote="\"", comment.char="")
featNames <- as.character(t(featNames))
featNames <- str_replace_all(featNames,"\\(abs\\)","_A_")
featNames[duplicated(featNames)] <- paste(featNames[duplicated(featNames)],"D",sep="_")

rep_ID <- numeric(nrow(Activitydata))
ctr <- 1
for (ind in c(1:(nrow(Activitydata)-1)))
{
  rep_ID[ind] <- ctr
  if (Activitydata$V1[ind] != Activitydata$V1[ind+1]) ctr <- 0;
  ctr <- ctr + 1
}
rownames(Activitydata) <- paste(Activitydata$V1,rep_ID,sep="_")
colnames(Activitydata) <- c("ID",featNames,"class")
Activitydata$rep <- rep_ID
  
tb <- table(Activitydata$class)

classes <- c("Neu","Emo","Men","Phy")
names(classes) <- names(tb)
Activitydata$class <- classes[as.character(Activitydata$class)]
table(Activitydata$class)
#> 
#>  Emo  Men  Neu  Phy 
#> 1120 1120 1120 1120



ID_class <- paste(Activitydata$ID,Activitydata$class)
IDCLASS <- unique(ID_class)
theclass <- Activitydata$class[!duplicated(ID_class)]
theIDs <- Activitydata$ID[!duplicated(ID_class)]

ActivitydataAvg <- NULL
for (id in IDCLASS)
{
  ActivitydataAvg <- rbind(ActivitydataAvg,apply(Activitydata[ID_class==id,featNames],2,mean))
}
colnames(ActivitydataAvg) <- featNames
rownames(ActivitydataAvg) <- IDCLASS
ActivitydataAvg <- as.data.frame(ActivitydataAvg)
ActivitydataAvg$class <- theclass
ActivitydataAvg$ID <- theIDs

table(ActivitydataAvg$class)
#> 
#> Emo Men Neu Phy 
#>  40  40  40  40


ActivitydataAvg <- subset(ActivitydataAvg, class=="Men" | class=="Emo")

ActivitydataAvg$class <- 1*(ActivitydataAvg$class == "Men")
table(ActivitydataAvg$class)
#> 
#>  0  1 
#> 40 40

1.2.0.1 Standarize the names for the reporting

studyName <- "Activity"
dataframe <- ActivitydataAvg
outcome <- "class"

TopVariables <- 10
thro <- 0.80
cexheat = 0.15

1.3 Generaring the report

1.3.1 Libraries

Some libraries

library(psych)
library(whitening)
library("vioplot")

1.3.2 Data specs

pander::pander(c(rows=nrow(dataframe),col=ncol(dataframe)-1))
rows col
80 534
pander::pander(table(dataframe[,outcome]))
0 1
40 40

varlist <- colnames(dataframe)
varlist <- varlist[varlist != outcome]

largeSet <- length(varlist) > 1000

1.3.3 Scaling the data

Scaling and removing near zero variance columns and highly co-linear(r>0.99999) columns


  ### Some global cleaning
  sdiszero <- apply(dataframe,2,sd) > 1.0e-16
  dataframe <- dataframe[,sdiszero]

  varlist <- colnames(dataframe)[colnames(dataframe) != outcome]
  tokeep <- c(as.character(correlated_Remove(dataframe,varlist,thr=0.99999)),outcome)
  dataframe <- dataframe[,tokeep]

  varlist <- colnames(dataframe)
  varlist <- varlist[varlist != outcome]


dataframe <- FRESAScale(dataframe,method="OrderLogit")$scaledData

1.4 The heatmap of the data


if (!largeSet)
{
  
  hm <- heatMaps(data=dataframe,
                 Outcome=outcome,
                 Scale=TRUE,
                 hCluster = "row",
                 xlab="Feature",
                 ylab="Sample",
                 srtCol=45,
                 srtRow=45,
                 cexCol=cexheat,
                 cexRow=cexheat
                 )
  par(op)
}

1.4.0.1 Correlation Matrix of the Data

The heat map of the data


if (!largeSet)
{

  par(cex=0.6,cex.main=0.85,cex.axis=0.7)
  #cormat <- Rfast::cora(as.matrix(dataframe[,varlist]),large=TRUE)
  cormat <- cor(dataframe[,varlist],method="pearson")
  cormat[is.na(cormat)] <- 0
  gplots::heatmap.2(abs(cormat),
                    trace = "none",
  #                  scale = "row",
                    mar = c(5,5),
                    col=rev(heat.colors(5)),
                    main = "Original Correlation",
                    cexRow = cexheat,
                    cexCol = cexheat,
                     srtCol=45,
                     srtRow=45,
                    key.title=NA,
                    key.xlab="Pearson Correlation",
                    xlab="Feature", ylab="Feature")
  diag(cormat) <- 0
  print(max(abs(cormat)))
}

[1] 1

1.5 The decorrelation


DEdataframe <- IDeA(dataframe,verbose=TRUE,thr=thro)
#> 
#>  Included: 362 , Uni p: 0.01628202 , Uncorrelated Base: 73 , Outcome-Driven Size: 0 , Base Size: 73 
#> 
#> 
 1 <R=1.000,w=  1,N=  265>, Top: 49( 21 )[ 1 : 49 : 0.975 ]( 48 , 185 , 0 ),<|>Tot Used: 233 , Added: 185 , Zero Std: 0 , Max Cor: 1.000
#> 
 2 <R=1.000,w=  1,N=  265>, Top: 26( 6 )[ 1 : 26 : 0.975 ]( 26 , 93 , 48 ),<|>Tot Used: 258 , Added: 93 , Zero Std: 0 , Max Cor: 1.000
#> 
 3 <R=1.000,w=  1,N=  265>, Top: 11( 2 )[ 1 : 11 : 0.975 ]( 10 , 18 , 74 ),<|>Tot Used: 258 , Added: 18 , Zero Std: 0 , Max Cor: 1.000
#> 
 4 <R=1.000,w=  2,N=   92>, Top: 35( 1 )[ 1 : 35 : 0.950 ]( 35 , 47 , 84 ),<|>Tot Used: 279 , Added: 47 , Zero Std: 0 , Max Cor: 0.993
#> 
 5 <R=0.993,w=  2,N=   92>, Top: 11( 1 )[ 1 : 11 : 0.947 ]( 11 , 11 , 101 ),<|>Tot Used: 280 , Added: 11 , Zero Std: 0 , Max Cor: 0.946
#> 
 6 <R=0.946,w=  2,N=   92>, Top: 19( 1 )[ 1 : 19 : 0.923 ]( 18 , 19 , 109 ),<|>Tot Used: 290 , Added: 19 , Zero Std: 0 , Max Cor: 0.943
#> 
 7 <R=0.943,w=  2,N=   92>, Top: 3( 1 )[ 1 : 3 : 0.921 ]( 3 , 3 , 114 ),<|>Tot Used: 292 , Added: 3 , Zero Std: 0 , Max Cor: 0.920
#> 
 8 <R=0.920,w=  3,N=  102>, Top: 44( 1 )[ 1 : 44 : 0.860 ]( 43 , 49 , 117 ),<|>Tot Used: 309 , Added: 49 , Zero Std: 0 , Max Cor: 0.983
#> 
 9 <R=0.983,w=  3,N=  102>, Top: 5( 1 )[ 1 : 5 : 0.892 ]( 5 , 6 , 135 ),<|>Tot Used: 309 , Added: 6 , Zero Std: 0 , Max Cor: 0.957
#> 
 10 <R=0.957,w=  4,N=   44>, Top: 21( 1 )[ 1 : 21 : 0.829 ]( 20 , 22 , 136 ),<|>Tot Used: 313 , Added: 22 , Zero Std: 0 , Max Cor: 0.892
#> 
 11 <R=0.892,w=  4,N=   44>, Top: 17( 1 )[ 1 : 17 : 0.800 ]( 17 , 18 , 143 ),<|>Tot Used: 316 , Added: 18 , Zero Std: 0 , Max Cor: 0.988
#> 
 12 <R=0.988,w=  4,N=   44>, Top: 2( 1 )[ 1 : 2 : 0.844 ]( 2 , 2 , 150 ),<|>Tot Used: 316 , Added: 2 , Zero Std: 0 , Max Cor: 0.994
#> 
 13 <R=0.994,w=  5,N=    4>, Top: 2( 1 )[ 1 : 2 : 0.800 ]( 2 , 2 , 151 ),<|>Tot Used: 316 , Added: 2 , Zero Std: 0 , Max Cor: 0.951
#> 
 14 <R=0.951,w=  6,N=    2>, Top: 1( 1 )[ 1 : 1 : 0.800 ]( 1 , 1 , 152 ),<|>Tot Used: 316 , Added: 1 , Zero Std: 0 , Max Cor: 0.799
#> 
 15 <R=0.000,w=  7,N=    0>
#> 
 [ 15 ], 0.7986104 Decor Dimension: 316 . Cor to Base: 196 , ABase: 33 , Outcome Base: 0 
#> 
varlistc <- colnames(DEdataframe)[colnames(DEdataframe) != outcome]

pander::pander(sum(apply(dataframe[,varlist],2,var)))

616

pander::pander(sum(apply(DEdataframe[,varlistc],2,var)))

164

pander::pander(entropy(discretize(unlist(dataframe[,varlist]), 256)))

4.84

pander::pander(entropy(discretize(unlist(DEdataframe[,varlistc]), 256)))

3.39

1.5.1 The decorrelation matrix


if (!largeSet)
{

  par(cex=0.6,cex.main=0.85,cex.axis=0.7)
  
  UPSTM <- attr(DEdataframe,"UPSTM")
  
  gplots::heatmap.2(1.0*(abs(UPSTM)>0),
                    trace = "none",
                    mar = c(5,5),
                    col=rev(heat.colors(5)),
                    main = "Decorrelation matrix",
                    cexRow = cexheat,
                    cexCol = cexheat,
                   srtCol=45,
                   srtRow=45,
                    key.title=NA,
                    key.xlab="|Beta|>0",
                    xlab="Output Feature", ylab="Input Feature")
  
  par(op)
}

1.6 The heatmap of the decorrelated data

if (!largeSet)
{

  hm <- heatMaps(data=DEdataframe,
                 Outcome=outcome,
                 Scale=TRUE,
                 hCluster = "row",
                 cexRow = cexheat,
                 cexCol = cexheat,
                 srtCol=45,
                 srtRow=45,
                 xlab="Feature",
                 ylab="Sample")
  par(op)
}

1.7 The correlation matrix after decorrelation

if (!largeSet)
{

  cormat <- cor(DEdataframe[,varlistc],method="pearson")
  cormat[is.na(cormat)] <- 0
  
  gplots::heatmap.2(abs(cormat),
                    trace = "none",
                    mar = c(5,5),
                    col=rev(heat.colors(5)),
                    main = "Correlation after IDeA",
                    cexRow = cexheat,
                    cexCol = cexheat,
                     srtCol=45,
                     srtRow=45,
                    key.title=NA,
                    key.xlab="Pearson Correlation",
                    xlab="Feature", ylab="Feature")
  
  par(op)
  diag(cormat) <- 0
  print(max(abs(cormat)))
}

[1] 0.997621

1.8 U-MAP Visualization of features

1.8.1 The UMAP based on LASSO on Raw Data

classes <- unique(dataframe[,outcome])
raincolors <- rainbow(length(classes))
names(raincolors) <- classes
datasetframe.umap = umap(scale(dataframe[,varlist]),n_components=2)
plot(datasetframe.umap$layout,xlab="U1",ylab="U2",main="UMAP: Original",t='n')
text(datasetframe.umap$layout,labels=dataframe[,outcome],col=raincolors[dataframe[,outcome]+1])

1.8.2 The decorralted UMAP


datasetframe.umap = umap(scale(DEdataframe[,varlistc]),n_components=2)
plot(datasetframe.umap$layout,xlab="U1",ylab="U2",main="UMAP: After IDeA",t='n')
text(datasetframe.umap$layout,labels=DEdataframe[,outcome],col=raincolors[DEdataframe[,outcome]+1])

1.9 Univariate Analysis

1.9.1 Univariate



univarRAW <- uniRankVar(varlist,
               paste(outcome,"~1"),
               outcome,
               dataframe,
               rankingTest="AUC")

100 : ECG_p_LF_mean 200 : IT_CCV_LF 300 : EDA_Original_mad_D




univarDe <- uniRankVar(varlistc,
               paste(outcome,"~1"),
               outcome,
               DEdataframe,
               rankingTest="AUC",
               )

100 : La_ECG_p_LF_mean 200 : La_IT_CCV_LF 300 : La_EDA_Original_mad_D

1.9.2 Final Table


univariate_columns <- c("caseMean","caseStd","controlMean","controlStd","controlKSP","ROCAUC")

##topfive
topvar <- c(1:length(varlist)) <= TopVariables
pander::pander(univarRAW$orderframe[topvar,univariate_columns])
  caseMean caseStd controlMean controlStd controlKSP ROCAUC
ECG_hrv_prctile75 0.483 1.031 -0.1471 1.286 0.0849 0.731
ECG_hrv_geomean_A_ -0.105 1.208 0.5327 1.093 0.1001 0.727
IT_LF_baseline_D 0.518 1.044 -0.2219 0.700 0.5845 0.721
IT_p_Total_baseline 0.507 1.009 -0.2168 0.668 0.6797 0.721
IT_VLF_baseline 0.492 0.990 -0.2217 0.652 0.7285 0.720
ECG_hrv_prctile25 0.244 1.150 -0.4451 0.938 0.5416 0.719
IT_PSD_baseline 0.554 1.095 -0.1829 0.773 0.2875 0.715
ECG_hrv_mean 0.259 0.865 -0.3517 0.931 0.2559 0.714
IT_HF_baseline 0.692 1.254 -0.0466 1.052 0.0555 0.713
ECG_hrv_trimmean25 0.282 0.950 -0.3372 0.967 0.5083 0.711


topLAvar <- univarDe$orderframe$Name[str_detect(univarDe$orderframe$Name,"La_")]
topLAvar <- unique(c(univarDe$orderframe$Name[topvar],topLAvar[1:as.integer(TopVariables/2)]))
finalTable <- univarDe$orderframe[topLAvar,univariate_columns]

theLaVar <- rownames(finalTable)[str_detect(rownames(finalTable),"La_")]

pander::pander(univarDe$orderframe[topLAvar,univariate_columns])
  caseMean caseStd controlMean controlStd controlKSP ROCAUC
La_EDA_Original_baseline_D -0.17678 0.3481 0.1236 0.3119 0.16752 0.772
La_IT_BRV_baseline -0.14953 0.4231 0.2053 0.2854 0.96581 0.762
La_EDA_Original_prctile75_D -0.00243 0.0214 0.0136 0.0208 0.00621 0.754
La_ECG_RR_window_baseline 0.13890 0.2973 -0.0886 0.2857 0.79161 0.747
La_EDA_Original_mad_D -0.01395 0.0530 0.0239 0.0323 0.39741 0.740
La_EDA_processed_std_D 0.24655 0.3816 -0.0878 0.3303 0.17461 0.724
IT_VLF_baseline 0.49163 0.9899 -0.2217 0.6524 0.72855 0.720
ECG_hrv_mean 0.25862 0.8655 -0.3517 0.9306 0.25586 0.714
La_EDA_processed_prctile25_D -0.25362 0.5637 0.1721 0.4719 0.39964 0.699
La_EDA_processed_median_D -0.29650 0.6597 0.0421 0.2853 0.13809 0.694

dc <- getLatentCoefficients(DEdataframe)
fscores <- attr(DEdataframe,"fscore")

theSigDc <- dc[theLaVar]
names(theSigDc) <- NULL
theSigDc <- unique(names(unlist(theSigDc)))


theFormulas <- dc[rownames(finalTable)]
deFromula <- character(length(theFormulas))
names(deFromula) <- rownames(finalTable)

pander::pander(c(mean=mean(sapply(dc,length)),total=length(dc),fraction=length(dc)/(ncol(dataframe)-1)))
mean total fraction
2.79 277 0.759


allSigvars <- names(dc)



dx <- names(deFromula)[1]
for (dx in names(deFromula))
{
  coef <- theFormulas[[dx]]
  cname <- names(theFormulas[[dx]])
  names(cname) <- cname
  for (cf in names(coef))
  {
    if (cf != dx)
    {
      if (coef[cf]>0)
      {
        deFromula[dx] <- paste(deFromula[dx],
                               sprintf("+ %5.3f*%s",coef[cf],cname[cf]))
      }
      else
      {
        deFromula[dx] <- paste(deFromula[dx],
                               sprintf("%5.3f*%s",coef[cf],cname[cf]))
      }
    }
  }
}

finalTable <- rbind(finalTable,univarRAW$orderframe[theSigDc[!(theSigDc %in% rownames(finalTable))],univariate_columns])


orgnamez <- rownames(finalTable)
orgnamez <- str_remove_all(orgnamez,"La_")
finalTable$RAWAUC <- univarRAW$orderframe[orgnamez,"ROCAUC"]
finalTable$DecorFormula <- deFromula[rownames(finalTable)]
finalTable$fscores <- fscores[rownames(finalTable)]

Final_Columns <- c("DecorFormula","caseMean","caseStd","controlMean","controlStd","controlKSP","ROCAUC","RAWAUC","fscores")

finalTable <- finalTable[order(-finalTable$ROCAUC),]
pander::pander(finalTable[,Final_Columns])
  DecorFormula caseMean caseStd controlMean controlStd controlKSP ROCAUC RAWAUC fscores
La_EDA_Original_baseline_D -1.053EDA_Original_prctile25_D + 1.000EDA_Original_baseline_D -0.17678 0.3481 0.12363 0.3119 0.16752 0.772 0.496 0
La_IT_BRV_baseline -0.833IT_BRV_mean + 1.000IT_BRV_baseline -0.14953 0.4231 0.20534 0.2854 0.96581 0.762 0.642 -1
La_EDA_Original_prctile75_D -0.456EDA_Original_max_D -0.544EDA_Original_prctile25_D + 1.000*EDA_Original_prctile75_D -0.00243 0.0214 0.01360 0.0208 0.00621 0.754 0.570 5
La_ECG_RR_window_baseline -0.892ECG_RR_window_trimmean25 + 1.000ECG_RR_window_baseline 0.13890 0.2973 -0.08862 0.2857 0.79161 0.747 0.522 0
La_EDA_Original_mad_D -1.026EDA_Original_std_D + 1.000EDA_Original_mad_D -0.01395 0.0530 0.02389 0.0323 0.39741 0.740 0.507 0
La_EDA_processed_std_D -0.890EDA_Original_std_D + 1.000EDA_processed_std_D 0.24655 0.3816 -0.08778 0.3303 0.17461 0.724 0.581 2
IT_VLF_baseline 0.49163 0.9899 -0.22167 0.6524 0.72855 0.720 0.720 5
ECG_hrv_mean 0.25862 0.8655 -0.35169 0.9306 0.25586 0.714 0.714 4
La_EDA_processed_prctile25_D + 0.996EDA_Original_std_D + 1.000EDA_processed_prctile25_D -0.25362 0.5637 0.17213 0.4719 0.39964 0.699 0.588 0
La_EDA_processed_median_D -0.906EDA_processed_trimmean25_D + 1.000EDA_processed_median_D -0.29650 0.6597 0.04206 0.2853 0.13809 0.694 0.530 -1
IT_BRV_baseline NA -0.35971 0.8468 -0.00106 1.0856 0.08274 0.642 0.642 NA
ECG_RR_window_trimmean25 NA -0.08973 0.9854 0.23086 0.9766 0.96455 0.606 0.606 12
EDA_processed_prctile25_D NA -0.65968 1.3545 -0.25260 1.0934 0.02142 0.588 0.588 NA
EDA_processed_trimmean25_D NA -0.39525 1.3811 -0.61752 1.3648 0.01157 0.582 0.582 2
EDA_processed_std_D NA 0.60948 1.1881 0.29184 0.9118 0.01488 0.581 0.581 NA
EDA_Original_max_D NA 0.63696 1.3434 0.36718 1.1892 0.00726 0.575 0.575 NA
EDA_Original_prctile25_D NA 0.66082 1.4080 0.37549 1.2295 0.00966 0.573 0.573 34
EDA_Original_prctile75_D NA 0.64742 1.3754 0.38526 1.2143 0.01041 0.570 0.570 NA
EDA_processed_median_D NA -0.65445 1.3990 -0.51718 1.3120 0.00378 0.530 0.530 NA
ECG_RR_window_baseline NA 0.05886 0.9529 0.11732 0.9276 0.77168 0.522 0.522 NA
EDA_Original_mad_D NA 0.40428 1.0948 0.46135 1.1988 0.01686 0.507 0.507 NA
IT_BRV_mean NA -0.25243 1.0855 -0.24788 1.0873 0.27583 0.503 0.503 5
EDA_Original_baseline_D NA 0.51887 1.4468 0.51891 1.4457 0.00876 0.496 0.496 NA
EDA_Original_std_D NA 0.40771 1.0812 0.42646 1.1545 0.02035 0.490 0.490 10